package com.coding4play.mqtt.broker.protocol;

import com.coding4play.mqtt.broker.config.BrokerProperties;
import com.coding4play.mqtt.broker.internal.AggregatedSink;
import com.coding4play.mqtt.common.auth.IAuthService;
import com.coding4play.mqtt.common.repository.*;
import lombok.Getter;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 协议处理
 */
@Component
@Getter
public class ProtocolProcess {

    private IAuthService authService;
    private IMessageIdService messageIdService;
    private AggregatedSink aggregatedSink;
    private BrokerProperties brokerProperties;

    private ISessionRepository sessionRepository;
    private ISubMessageRepository subMessageRepository;
    private IRetainMessageRepository retainMessageRepository;
    private IDupPublishMessageRepository dupPublishMessageRepository;
    private IDupPubRelMessageRepository dupPubRelMessageRepository;

    private Connect connect;
    private Subscribe subscribe;
    private UnSubscribe unSubscribe;
    private Publish publish;
    private DisConnect disConnect;
    private PingReq pingReq;
    private PubRel pubRel;
    private PubAck pubAck;
    private PubRec pubRec;
    private PubComp pubComp;

    public ProtocolProcess(IAuthService authService, IMessageIdService messageIdService, AggregatedSink aggregatedSink, BrokerProperties brokerProperties, ISessionRepository sessionRepository, ISubMessageRepository subMessageRepository, IRetainMessageRepository retainMessageRepository, IDupPublishMessageRepository dupPublishMessageRepository, IDupPubRelMessageRepository dupPubRelMessageRepository) {
        this.authService = authService;
        this.messageIdService = messageIdService;
        this.aggregatedSink = aggregatedSink;
        this.brokerProperties = brokerProperties;
        this.sessionRepository = sessionRepository;
        this.subMessageRepository = subMessageRepository;
        this.retainMessageRepository = retainMessageRepository;
        this.dupPublishMessageRepository = dupPublishMessageRepository;
        this.dupPubRelMessageRepository = dupPubRelMessageRepository;
    }

    @PostConstruct
    public void postConstruct() {
        connect = new Connect(sessionRepository, subMessageRepository, dupPublishMessageRepository, dupPubRelMessageRepository, authService, brokerProperties);

        subscribe = new Subscribe(subMessageRepository, messageIdService, retainMessageRepository);
        unSubscribe = new UnSubscribe(subMessageRepository);

        publish = new Publish(sessionRepository, subMessageRepository, messageIdService, retainMessageRepository, dupPublishMessageRepository, aggregatedSink, brokerProperties);
        pubAck = new PubAck(dupPublishMessageRepository);

        pubRec = new PubRec(dupPublishMessageRepository, dupPubRelMessageRepository);
        pubRel = new PubRel();
        pubComp = new PubComp(dupPubRelMessageRepository);

        pingReq = new PingReq(sessionRepository, brokerProperties);
        disConnect = new DisConnect(sessionRepository, subMessageRepository, dupPublishMessageRepository, dupPubRelMessageRepository);

    }
}

